package cn.cdeden.resource.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.core.page.PageQuery;
import cn.cdeden.resource.domain.vo.SysOssVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.cdeden.resource.domain.bo.SysOssVersionBo;
import cn.cdeden.resource.domain.vo.SysOssVersionVo;
import cn.cdeden.resource.domain.SysOssVersion;
import cn.cdeden.resource.mapper.SysOssVersionMapper;
import cn.cdeden.resource.service.ISysOssVersionService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * OSS对象存储版本处理Service业务层处理
 *
 * @author Yu.
 * @date 2024-12-04
 */
@RequiredArgsConstructor
@Service
public class SysOssVersionServiceImpl implements ISysOssVersionService {

    private final SysOssVersionMapper baseMapper;


    @Override
    public SysOssVersionVo buildResultEntity(SysOssVo sysOssVo) {
        SysOssVersion ossVersion = MapstructUtils.convert(sysOssVo, SysOssVersion.class);
        ossVersion.setCreateBy(sysOssVo.getCreateBy());
        ossVersion.setCreateDept(sysOssVo.getCreateDept());
        ossVersion.setCreateTime(null);
        int insert = baseMapper.insert(ossVersion);
        SysOssVersionVo sysOssVersionVo = MapstructUtils.convert(ossVersion, SysOssVersionVo.class);
        return sysOssVersionVo;
    }

    @Override
    public SysOssVersionVo getByIdAndVersionId(Long ossId, String versionId) {
        SysOssVersionVo sysOssVersionVo = baseMapper.selectVoOne(Wrappers.lambdaQuery(SysOssVersion.class)
            .eq(SysOssVersion::getOssId, ossId)
            .eq(SysOssVersion::getVersionId, versionId)
        );
        return sysOssVersionVo;
    }

    @Override
    public List<SysOssVersionVo> getByIdList(Long ossId) {
        List<SysOssVersionVo> list = baseMapper.selectVoList(Wrappers.lambdaQuery(SysOssVersion.class)
            .eq(SysOssVersion::getOssId, ossId)

        );
        return list;
    }

    @Override
    public List<SysOssVersionVo> getByIdsList(Collection<Long> ossIds) {
        List<SysOssVersionVo> versionVos = baseMapper.selectVoList(Wrappers.lambdaQuery(SysOssVersion.class)
            .in(SysOssVersion::getOssId, ossIds)
            .orderByDesc(SysOssVersion::getVersionName)
        );


        return versionVos;
    }

    @Override
    public TableDataInfo<SysOssVersionVo> getByIdPage(Long ossId, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOssVersion> lqw = Wrappers.lambdaQuery(
            SysOssVersion.class)
            .eq(SysOssVersion::getOssId, ossId)
            .orderByDesc(SysOssVersion::getVersionName)
            ;

        Page<SysOssVersionVo> page = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @Override
    public Boolean deleteWithValidByOssIds(Collection<Long> ossId, Boolean isValid) {
        int delete = baseMapper.delete(Wrappers.lambdaQuery(SysOssVersion.class)
            .in(SysOssVersion::getOssId, ossId)
        );
        return delete > 0;
    }

    @Override
    public Boolean deleteByOssIdAndVersionId(Long ossId, String versionId) {
        int delete = baseMapper.delete(Wrappers.lambdaQuery(SysOssVersion.class)
            .eq(SysOssVersion::getOssId, ossId)
            .eq(SysOssVersion::getVersionId, versionId)
        );
        return delete > 0;
    }


}
